/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Control;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.annotation.XmlRootElement;


/**
 *
 * @author ANTV60287
 */
@XmlRootElement
public class UserObject {
    private String username;
    private String password;
    private String fullName;
    private String address;
    private String email;
    private String telephone;

    public UserObject(String username, String password, String fullName, String address, String email, String telephone) {
        this.username = username;
        this.password = password;
        this.fullName = fullName;
        this.address = address;
        this.email = email;
        this.telephone = telephone;
    }

    public UserObject(String username, String password){
        this.username = username;
        this.password = password;
    }

    public UserObject() {
    }
   
    public static UserObject checkLogin(String username, String password) {
        Connection con = null;
        PreparedStatement stm = null;
        ResultSet rs = null;
        UserObject user = null;
        try {
            Config.Connection config = new Config.Connection();
            con = config.getConnection();
            String sql = "select * from [tblCustomer] where Username ='" + username + "' AND Password ='" + password + "'";
            stm = con.prepareCall(sql);
            rs = stm.executeQuery();
            while (rs.next()) {
                user = new UserObject(rs.getNString("Username"), rs.getNString("Password"));
            }
            return user;
        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (stm != null) {
                    stm.close();
                }
                if (con != null) {
                    con.close();
                }
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return null;
    }
    public void Register(String Username, String Password, String FullName, String Address, String Email, String Telephone) {
        try {
        Connection con = null;      
            Config.Connection config = new Config.Connection();
            con = config.getConnection();
            String sql = "INSERT INTO tblCustomer(Username,Password,CustomerName,CustomerAddress,CustomerEmail,CustomerPhone)"
                    + " VALUES('" + Username + "','" + Password + "','" + FullName + "','" + Address + "','" + Email + "','" + Telephone + "')";
            Statement stm = con.createStatement();
            stm.executeUpdate(sql);
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(UserObject.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(UserObject.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public String hashPassword(String password) {
        String hashword = null;
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(password.getBytes());
            BigInteger hash = new BigInteger(1, md5.digest());
            hashword = hash.toString(16);

        } catch (NoSuchAlgorithmException nsae) {
        }
        return hashword;
    }
   
    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }

    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return the fullName
     */
    public String getFullName() {
        return fullName;
    }

    /**
     * @param fullName the fullName to set
     */
    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    /**
     * @return the address
     */
    public String getAddress() {
        return address;
    }

    /**
     * @param address the address to set
     */
    public void setAddress(String address) {
        this.address = address;
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * @return the telephone
     */
    public String getTelephone() {
        return telephone;
    }

    /**
     * @param telephone the telephone to set
     */
    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

}
